Skip to content

Method: lambda$1(Descriptor, OWLClassA)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.test.runner;
16:
17: import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
18: import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
19: import cz.cvut.kbss.jopa.model.descriptors.ObjectPropertyCollectionDescriptor;
20: import cz.cvut.kbss.jopa.test.*;
21: import cz.cvut.kbss.jopa.test.environment.Generators;
22: import cz.cvut.kbss.jopa.test.environment.TestEnvironmentUtils;
23: import org.junit.Test;
24: import org.slf4j.Logger;
25:
26: import java.net.URI;
27: import java.util.ArrayList;
28: import java.util.Collections;
29: import java.util.Iterator;
30: import java.util.List;
31:
32: import static org.junit.Assert.*;
33:
34: public abstract class UpdateOperationsMultiContextRunner extends BaseRunner {
35:
36: public UpdateOperationsMultiContextRunner(Logger logger) {
37: super(logger);
38: }
39:
40: @Test
41: public void testUpdateDataPropertyInContext() throws Exception {
42: logger.debug("Test: update data property value which is stored in a different context that the owner.");
43: this.em = getEntityManager("MultiUpdateDataPropertyInContext", false);
44: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
45: aDescriptor.addAttributeContext(OWLClassA.class.getDeclaredField("stringAttribute"), CONTEXT_TWO);
46: em.getTransaction().begin();
47: em.persist(entityA, aDescriptor);
48: em.getTransaction().commit();
49:
50: em.getTransaction().begin();
51: final OWLClassA a = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
52: assertNotNull(a);
53: final String newAttValue = "newStringAttributeValue";
54: a.setStringAttribute(newAttValue);
55: em.getTransaction().commit();
56:
57: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
58: assertNotNull(resA);
59: assertEquals(newAttValue, resA.getStringAttribute());
60: assertEquals(entityA.getTypes(), resA.getTypes());
61: }
62:
63: @Test
64: public void testUpdateObjectPropertyToDifferentContext() throws Exception {
65: logger.debug("Test: update object property with value from different context than the previous.");
66: this.em = getEntityManager("MultiUpdateObjectPropertyToDifferent", false);
67: final Descriptor dDescriptor = new EntityDescriptor();
68: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_ONE);
69: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), aDescriptor);
70: em.getTransaction().begin();
71: em.persist(entityD, dDescriptor);
72: em.persist(entityA, aDescriptor);
73: em.getTransaction().commit();
74:
75: final OWLClassD d = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
76: assertNotNull(d);
77: assertNotNull(d.getOwlClassA());
78: em.getTransaction().begin();
79: final OWLClassA newA = new OWLClassA();
80: newA.setUri(URI.create("http://krizik.felk.cvut.cz/jopa/ontologies/newEntityA"));
81: newA.setStringAttribute("newAStringAttribute");
82: final Descriptor newADescriptor = new EntityDescriptor(CONTEXT_TWO);
83: em.persist(newA, newADescriptor);
84: dDescriptor.addAttributeDescriptor(OWLClassD.class.getDeclaredField("owlClassA"), newADescriptor);
85: d.setOwlClassA(newA);
86: em.getTransaction().commit();
87:
88: final OWLClassD resD = em.find(OWLClassD.class, entityD.getUri(), dDescriptor);
89: assertNotNull(resD);
90: assertEquals(newA.getUri(), resD.getOwlClassA().getUri());
91: assertEquals(newA.getStringAttribute(), resD.getOwlClassA().getStringAttribute());
92: final OWLClassA resA = em.find(OWLClassA.class, entityA.getUri(), aDescriptor);
93: assertNotNull(resA);
94: assertEquals(entityA.getStringAttribute(), resA.getStringAttribute());
95: }
96:
97: @Test
98: public void testUpdateAddToPropertiesInContext() throws Exception {
99: logger.debug("Test: add new property value, properties are stored in a different context.");
100: this.em = getEntityManager("MultiUpdateAddToPropertiesInContext", false);
101: entityB.setProperties(Generators.createProperties());
102: final Descriptor bDescriptor = new EntityDescriptor(CONTEXT_ONE);
103: bDescriptor.addAttributeContext(OWLClassB.class.getDeclaredField("properties"), CONTEXT_TWO);
104: em.getTransaction().begin();
105: em.persist(entityB, bDescriptor);
106: em.getTransaction().commit();
107:
108: em.getTransaction().begin();
109: final OWLClassB b = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
110: assertNotNull(b);
111: final String newKey = "http://krizik.felk.cvut.cz/jopa/ontologies/properties/newPropertyKey";
112: final String newValue = "http://krizik.felk.cvut.cz/jopa/ontologies/newPropertyValue";
113: final String newPropertyValue = "http://krizik.felk.cvut.cz/jopa/ontologies/NewValueOfAnOldProperty";
114: final String propertyToChange = b.getProperties().keySet().iterator().next();
115: b.getProperties().put(newKey, Collections.singleton(newValue));
116: b.getProperties().get(propertyToChange).add(newPropertyValue);
117: em.getTransaction().commit();
118:
119: final OWLClassB res = em.find(OWLClassB.class, entityB.getUri(), bDescriptor);
120: assertNotNull(res);
121: assertEquals(entityB.getStringAttribute(), res.getStringAttribute());
122: assertTrue(TestEnvironmentUtils.arePropertiesEqual(b.getProperties(), res.getProperties()));
123: }
124:
125: @Test
126: public void testUpdateAddToSimpleListInContext() throws Exception {
127: logger.debug("Test: add new element into a simple list stored in different context than its owner.");
128: this.em = getEntityManager("MultiUpdateAddToSimpleListInContext", false);
129: entityC.setSimpleList(Generators.createSimpleList(15));
130: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
131: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
132: OWLClassC.getSimpleListField());
133: cDescriptor.addAttributeDescriptor(OWLClassC.getSimpleListField(), lstDescriptor);
134: em.getTransaction().begin();
135: em.persist(entityC, cDescriptor);
136: entityC.getSimpleList().forEach(a -> em.persist(a, lstDescriptor));
137: em.getTransaction().commit();
138:
139: em.getTransaction().begin();
140: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
141: assertNotNull(c);
142: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
143: c.getSimpleList().add(entityA);
144: em.persist(entityA, lstDescriptor);
145: em.getTransaction().commit();
146:
147: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
148: assertNotNull(resC);
149: assertEquals(entityC.getSimpleList().size() + 1, resC.getSimpleList().size());
150: boolean found = false;
151: for (OWLClassA a : resC.getSimpleList()) {
152: if (a.getUri().equals(entityA.getUri())) {
153: assertEquals(entityA.getStringAttribute(), a.getStringAttribute());
154: assertEquals(entityA.getTypes(), a.getTypes());
155: found = true;
156: break;
157: }
158: }
159: assertTrue(found);
160: }
161:
162: @Test
163: public void testUpdateAddToReferencedListInContext() throws Exception {
164: logger.debug("Test: add new element into a referenced list stored in different context than its owner.");
165: this.em = getEntityManager("MultiUpdateAddToReferencedListInContext", false);
166: entityC.setReferencedList(Generators.createReferencedList(10));
167: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
168: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
169: OWLClassC.getReferencedListField());
170: cDescriptor.addAttributeDescriptor(OWLClassC.getReferencedListField(), lstDescriptor);
171: em.getTransaction().begin();
172: em.persist(entityC, cDescriptor);
173: entityC.getReferencedList().forEach(a -> em.persist(a, lstDescriptor));
174: em.getTransaction().commit();
175:
176: em.getTransaction().begin();
177: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
178: assertNotNull(c);
179: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
180: c.getReferencedList().add(entityA);
181: em.persist(entityA, lstDescriptor);
182: em.getTransaction().commit();
183:
184: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
185: assertNotNull(resC);
186: assertEquals(entityC.getReferencedList().size() + 1, resC.getReferencedList().size());
187: boolean found = false;
188: for (OWLClassA a : resC.getReferencedList()) {
189: if (a.getUri().equals(entityA.getUri())) {
190: assertEquals(entityA.getStringAttribute(), a.getStringAttribute());
191: assertEquals(entityA.getTypes(), a.getTypes());
192: found = true;
193: break;
194: }
195: }
196: assertTrue(found);
197: }
198:
199: @Test
200: public void testUpdateRemoveFromSimpleListInContext() throws Exception {
201: logger.debug("Test: remove element from simple list stored in a different context than its owner.");
202: this.em = getEntityManager("MultiUpdateRemoveFromSimpleListInContext", false);
203: entityC.setSimpleList(Generators.createSimpleList(15));
204: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
205: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
206: OWLClassC.getSimpleListField());
207: cDescriptor.addAttributeDescriptor(OWLClassC.getSimpleListField(), lstDescriptor);
208: em.getTransaction().begin();
209: em.persist(entityC, cDescriptor);
210: entityC.getSimpleList().forEach(a -> em.persist(a, lstDescriptor));
211: em.getTransaction().commit();
212:
213: em.getTransaction().begin();
214: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
215: assertNotNull(c);
216: assertEquals(entityC.getSimpleList().size(), c.getSimpleList().size());
217: final OWLClassA a = c.getSimpleList().get(0);
218: c.getSimpleList().remove(0);
219: em.remove(a);
220: em.getTransaction().commit();
221:
222: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
223: assertNotNull(resC);
224: assertEquals(entityC.getSimpleList().size() - 1, resC.getSimpleList().size());
225: assertNull(em.find(OWLClassA.class, a.getUri(), lstDescriptor));
226: }
227:
228: @Test
229: public void testUpdateRemoveFromReferencedListInContext() throws Exception {
230: logger.debug("Test: remove elements from referenced list stored in a different context than its owner.");
231: this.em = getEntityManager("MultiUpdateRemoveFromReferencedListInContext", false);
232: entityC.setReferencedList(Generators.createReferencedList(10));
233: final Descriptor cDescriptor = new EntityDescriptor(CONTEXT_ONE);
234: final Descriptor lstDescriptor = new ObjectPropertyCollectionDescriptor(CONTEXT_TWO,
235: OWLClassC.getReferencedListField());
236: cDescriptor.addAttributeDescriptor(OWLClassC.getReferencedListField(), lstDescriptor);
237: em.getTransaction().begin();
238: em.persist(entityC, cDescriptor);
239: entityC.getReferencedList().forEach(a -> em.persist(a, lstDescriptor));
240: em.getTransaction().commit();
241:
242: em.getTransaction().begin();
243: final OWLClassC c = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
244: assertNotNull(c);
245: assertEquals(entityC.getReferencedList().size(), c.getReferencedList().size());
246: final List<OWLClassA> removed = new ArrayList<>();
247: int i = 0;
248: final Iterator<OWLClassA> it = c.getReferencedList().iterator();
249: while (it.hasNext()) {
250: i++;
251: final OWLClassA a = it.next();
252: if (i % 2 == 1) {
253: continue;
254: }
255: removed.add(a);
256: it.remove();
257: }
258: em.getTransaction().commit();
259:
260: final OWLClassC resC = em.find(OWLClassC.class, entityC.getUri(), cDescriptor);
261: assertNotNull(resC);
262: assertEquals(entityC.getReferencedList().size() - removed.size(), resC.getReferencedList()
263: .size());
264: for (OWLClassA a : removed) {
265: final OWLClassA resA = em.find(OWLClassA.class, a.getUri(), lstDescriptor);
266: assertNotNull(resA);
267: }
268: }
269:
270: @Test
271: public void testUpdatePlainIdentifierObjectPropertyValueInContext() {
272: final Descriptor pDescriptor = new EntityDescriptor(CONTEXT_ONE);
273: entityP.setIndividualUri(URI.create("http://krizik.felk.cvut.cz/originalIndividual"));
274: this.em = getEntityManager("UpdatePlainIdentifierObjectPropertyValueInContext", true);
275: em.getTransaction().begin();
276: em.persist(entityP, pDescriptor);
277: em.getTransaction().commit();
278:
279: final OWLClassP toUpdate = em.find(OWLClassP.class, entityP.getUri());
280: em.detach(toUpdate);
281: final URI newUri = URI.create("http://krizik.felk.cvut.cz/newIndividual");
282: toUpdate.setIndividualUri(newUri);
283: em.getTransaction().begin();
284: em.merge(toUpdate);
285: em.getTransaction().commit();
286:
287: final OWLClassP res = em.find(OWLClassP.class, entityP.getUri());
288: assertNotNull(res);
289: assertEquals(newUri, res.getIndividualUri());
290: }
291:
292: @Test
293: public void testUpdateFieldInMappedSuperclassInContext() throws Exception {
294: final Descriptor qDescriptor = new EntityDescriptor(CONTEXT_ONE);
295: final Descriptor aDescriptor = new EntityDescriptor(CONTEXT_TWO);
296: qDescriptor.addAttributeDescriptor(OWLClassQ.getOWlClassAField(), aDescriptor);
297: this.em = getEntityManager("UpdateFieldInMappedSuperclassInContext", true);
298: em.getTransaction().begin();
299: em.persist(entityQ, qDescriptor);
300: em.persist(entityA, aDescriptor);
301: em.getTransaction().commit();
302:
303: entityQ.setStringAttribute("newStringAttribute");
304: entityQ.setParentString("newParentStringAttribute");
305: entityQ.setLabel("newLabel");
306: final OWLClassA newA = new OWLClassA(URI.create("http://krizik.felk.cvut.cz/ontologies/jopa/tests/entityA2"));
307: newA.setStringAttribute("newAString");
308: entityQ.setOwlClassA(newA);
309: em.getTransaction().begin();
310: em.merge(entityQ, qDescriptor);
311: em.persist(newA, aDescriptor);
312: em.getTransaction().commit();
313:
314: final OWLClassQ res = em.find(OWLClassQ.class, entityQ.getUri(), qDescriptor);
315: assertNotNull(res);
316: assertEquals(entityQ.getStringAttribute(), res.getStringAttribute());
317: assertEquals(entityQ.getParentString(), res.getParentString());
318: assertEquals(entityQ.getLabel(), res.getLabel());
319: assertNotNull(res.getOwlClassA());
320: assertEquals(newA.getUri(), res.getOwlClassA().getUri());
321: assertNotNull(em.find(OWLClassA.class, newA.getUri()));
322: assertNotNull(em.find(OWLClassA.class, entityA.getUri(), aDescriptor));
323: }
324: }